using System.Collections.Generic;
using System.IO;
using System.Linq;
using xiushen.Domain;

namespace xiushen.Tests
{
    public class DataMigation
    {
        private XiushenRepository saveRepositry;
        private string path;
        public string FilePath { get { return path; } set { path = value; } }
        public DataMigation(string path)
        {
            saveRepositry = new XiushenRepository();
            this.path = path;
        }
        public void InsertFilesBasedOnOneCategory()
        {
            var files = System.IO.Directory.GetFiles(path, "*.pdf", SearchOption.AllDirectories);
            int id = 0;
            if (saveRepositry.QueryAll<Attachment>().Count() > 0)
            {
                id = saveRepositry.QueryAll<Attachment>().Max(r => r.ID);
            }

            var dics = saveRepositry.QueryAll<Category>().ToDictionary(r => r.Name, e => e.ID);
            foreach (var file in files)
            {
                var fileName = Path.GetFileName(file);
                var dictionary = Directory.GetParent(file).Name;
                var attach = new Attachment();

                //attach.Name = fileName;
                //attach.RelativePath = DXHanZiToPinYin.DXHanZiToPinYin.Convert( file.Replace(path, string.Empty),200);
                attach.ID = ++id;
                attach.CategoryID = dics[dictionary];
                saveRepositry.Insert(attach);
            }
        }

        public void CreateFoldersBasedOnCategoryName()
        {
            var allcategors = saveRepositry.QueryAll<Category>().Where(r => r.ParentID != null).ToDictionary(r => r.Name, e => e.ParentID.Value);
            var categorys = saveRepositry.QueryAll<Category>().Where(r => r.ParentID == null).ToDictionary(r => r.Name,e=>e.ID);
            CreateFoldersByCategory(path, categorys.Keys);
            foreach (var category in categorys)
            {
                KeyValuePair<string, int> category1 = category;
                var subs = allcategors.Where(r => r.Value == category1.Value).Select(r=>r.Key);
                var finalPath = Path.Combine(path, category.Key);
                CreateFoldersByCategory(finalPath, subs);

            }
         
        }
    
        public void CreateFoldersBasedOnCategoryDisplayName()
        {
            var allcategors = saveRepositry.QueryAll<Category>().Where(r => r.ParentID != null).ToDictionary(r => r.DisplayName, e => e.ParentID.Value);
            var categorys = saveRepositry.QueryAll<Category>().Where(r => r.ParentID == null).ToDictionary(r => r.DisplayName, e => e.ID);
            CreateFoldersByCategory(path, categorys.Keys);
            foreach (var category in categorys)
            {
                KeyValuePair<string, int> category1 = category;
                var subs = allcategors.Where(r => r.Value == category1.Value).Select(r => r.Key);
                var finalPath = Path.Combine(path, category.Key);
                CreateFoldersByCategory(finalPath, subs);

            }

        }
        private void CreateFoldersByCategory(string mpath ,IEnumerable<string> categorys)
        {
            foreach (var category in categorys)
            {
                var finalPath = Path.Combine(mpath, category);
                if (!System.IO.Directory.Exists(finalPath))
                {
                    System.IO.Directory.CreateDirectory(finalPath);
                }
            }

        }

        public void CopyFiles(string source,string dest)
        {
            var files = System.IO.Directory.GetFiles(source, "*.pdf", SearchOption.AllDirectories);
            foreach (var file in files)
            {
                var newFile = file.Replace(source, dest);
                File.Copy(file, DXHanZiToPinYin.DXHanZiToPinYin.Convert(newFile, 200));
            }
        }

    
    }
}